package net.entelijan.cobean.data.testwebapp;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitBook;

/**
 * Servlet implementation class Action
 */
public class Action extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Action() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("do");
		String pServiceName = "cobean-data-ear/PersistenceLitService/remote";
		String imServiceName = "cobean-data-ear/InMemLitService/remote";
		try {
			if ("showDefaultDS".equals(action)) {
				Context ctx = new InitialContext();
				Object obj = ctx.lookup("java:DefaultDS");
				request.setAttribute("info", "ds :" + obj);
			} else if ("showExtLitDS".equals(action)) {
				Context ctx = new InitialContext();
				Object obj = ctx.lookup("java:extLitDS");
				request.setAttribute("info", "ds :" + obj);
			} else if ("showDerbyPool".equals(action)) {
				InitialContext ic = new InitialContext();
				String dsName = "jdbc/__default";
				Object obj = ic.lookup(dsName);

				request.setAttribute("info", "DerbyPool :" + obj);
			} else {
				if ("showLitService".equals(action)) {
					Context ctx = new InitialContext();
					Object obj = ctx.lookup(imServiceName);
					request.setAttribute("info", imServiceName + " :" + obj);
				} else if ("showBook".equals(action)) {
					Context ctx = new InitialContext();
					ILitService ls = (ILitService) ctx.lookup(imServiceName);
					request.setAttribute("info", imServiceName + " :"
							+ ls.loadBook(4));
				} else if ("showBookPers".equals(action)) {
					Context ctx = new InitialContext();
					ILitService ls = (ILitService) ctx.lookup(pServiceName);
					request.setAttribute("info", pServiceName + " :"
							+ ls.loadBook(4l));
				} else if ("showAllBookPers".equals(action)) {
					Context ctx = new InitialContext();
					ILitService ls = (ILitService) ctx.lookup(pServiceName);
					request.setAttribute("info", pServiceName + " :"
							+ ls.loadAllBooks());
				} else if ("showAllAuthors".equals(action)) {
					Context ctx = new InitialContext();
					ILitService ls = (ILitService) ctx.lookup(pServiceName);
					request.setAttribute("info", pServiceName + " :"
							+ ls.loadAllAuthors());
				} else if ("insertBooks".equals(action)) {
					Context ctx = new InitialContext();
					ILitService pls = (ILitService) ctx.lookup(pServiceName);
					ILitService mls = (ILitService) ctx.lookup(imServiceName);
					int cnt = 0;
					for (LitBook b : mls.loadAllBooks()) {
						b.setAuthor(null);
						b.setId(-1);
						pls.saveBook(b);
						cnt++;
					}
					request.setAttribute("info", pServiceName + " : inserted "
							+ cnt + " books");
				} else {
					request.setAttribute("info", "Unknown Action");
				}
			}
		} catch (Exception e) {
			request.setAttribute("info", "Could execute action '" + action
					+ "' because: " + e.getMessage());
			e.printStackTrace();
		}
		request.getRequestDispatcher("index.jsp").forward(request, response);
	}

}
